package mws;

/**
 * Hello world!
 */
public class App {
    public static void main(String[] args) {

        System.out.println("Hello World!");
        int[] nums = {1, 0, 1, 0};
        boolean b = canJump(nums);

        System.out.println("Hello World!");

        new Thread(ALock::lock).start();

        new Thread(BLock::lock).start();
//leetcode submit region begin(Prohibit modification and deletion)
//        //leetcode submit region begin(Prohibit modification and deletion)
//        class Solution {
//
//
//        }
//
    }
    public static boolean canJump(int[] nums) {

        //  只要能跳到最后一位那 前面的都可以调到


        int length = nums.length;

        if (nums[0] == 0) {
            if (nums.length == 1)
                return true;
            return false;
        }
        if (length <= 0)
            return false;

        int index = length - 1;

        for (int i = length - 2; i >= 0; i--) {
            if (nums[i] >= (index - i)) {
                int record = i;
                while (record >= 0) {
                    if (nums[record] >= (index - record)) {
                        record--;
                    } else {
                        record++;
                        break;
                    }
                }
                index = record;
                i = record;
            }
        }

        if (index <= 0) return true;
        else return false;


    }

    static class ALock {

        public static synchronized void lock() {
            try {
                Thread.sleep(1000 * 10);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            BLock.lock();
            System.out.println("ALock lock");
        }

    }

    static class BLock {

        public static synchronized void lock() {
            try {
                Thread.sleep(1000 * 10);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            ALock.lock();
            System.out.println("BLock lock");
        }
    }

}
